package com.example.basecommon.utils;

import android.content.Context;
import android.content.SharedPreferences;

public class SPUtils {
    private SharedPreferences mPreferences;
    private static SPUtils mySharedPreferences;

    private SPUtils(Context context) {
        if (mPreferences == null) {
            mPreferences = context.getSharedPreferences("greatonce", Context.MODE_PRIVATE);
        }
    }

    // 单实例
    public static SPUtils getInstance(Context context) {
        if (mySharedPreferences == null) {
            mySharedPreferences = new SPUtils(context);
        }
        return mySharedPreferences;
    }

    public static String getUserName(Context context) {
        return getInstance(context).mPreferences.getString("userName", "");
    }

    public static void setUserName(Context context, String v) {
        getInstance(context).mPreferences.edit().putString("userName", v).commit();
    }

    public static String getServerUrl(Context context) {
        return getInstance(context).mPreferences.getString("serverUrl", "");
    }

    public static void setServerUrl(Context context, String v) {
        getInstance(context).mPreferences.edit().putString("serverUrl", v).commit();
    }

    public static String getCustomCode(Context context) {
        return getInstance(context).mPreferences.getString("customerCode", "");
    }

    public static void setCustomCode(Context context, String v) {
        getInstance(context).mPreferences.edit().putString("customerCode", v).commit();
    }

//    public static boolean isOpenOnlyCode(Context context) {
//        return getInstance(context).mPreferences.getBoolean("onlyCode", false);
//    }
//
//    public static void setOpenOnlyCode(Context context, boolean v) {
//        getInstance(context).mPreferences.edit().putBoolean("onlyCode", v).commit();
//    }
public static String getOnlyCodeDes(Context context) {
    return getInstance(context).mPreferences.getString("onlyCodeDes", "http://weixin.qq.com");
}

    public static void setOnlyCodeDes(Context context, String v) {
        getInstance(context).mPreferences.edit().putString("onlyCodeDes", v).commit();
    }
    public static String getOnlyCodeSpit(Context context) {
        return getInstance(context).mPreferences.getString("onlyCodeSpit", "-");
    }

    public static void setOnlyCodeSpit(Context context, String v) {
        getInstance(context).mPreferences.edit().putString("onlyCodeSpit", v).commit();
    }

    /**
     * 取String
     *
     * @param key
     * @return
     */
    public static String getStr(Context context, String key) {
        return getInstance(context).mPreferences.getString(key, "");
    }


    /**
     * 存储String
     *
     * @param key
     * @param v
     */
    public static void setStr(Context context, String key, String v) {
        getInstance(context).mPreferences.edit().putString(key, v).commit();
    }


    /**
     * 取String
     *
     * @param mPreferences
     * @param key
     * @return
     */
    public static String getStr(SPUtils mPreferences, String key) {
        return mPreferences.mPreferences.getString(key, "");
    }


    /**
     * 取int
     *
     * @param mPreferences
     * @param key
     * @return
     */
    public static int getInt(SPUtils mPreferences, String key) {
        return mPreferences.mPreferences.getInt(key, -1);
    }

    /**
     * 取int
     *
     * @param
     * @param key
     * @return
     */
    public static int getInt(Context context, String key) {
        return getInstance(context).mPreferences.getInt(key, -1);
    }

    /**
     * 存储String
     *
     * @param mPreferences
     * @param key
     * @param v
     */
    public static void putInt(SPUtils mPreferences, String key,
                              int v) {
        mPreferences.mPreferences.edit().putInt(key, v).commit();
    }


    /**
     * 存储String
     *
     * @param key
     * @param v
     */
    public static void putInt(Context context, String key, int v) {
        SPUtils.getInstance(context).mPreferences.edit().putInt(key, v).commit();

    }


    /**
     * 取Long
     *
     * @param key
     * @return
     */
    public static long getLong(Context context, String key) {
        return getInstance(context).mPreferences.getLong(key, -1);
    }


    /**
     * 存储String
     *
     * @param key
     * @param v
     */
    public static void putLong(Context context, String key,
                               long v) {
        getInstance(context).mPreferences.edit().putLong(key, v).commit();
    }


    /**
     * 存储String
     *
     * @param key
     * @param v
     */
    public static void putLong(Context context, String key, String v) {
        long l = 0;
        try {
            l = Long.parseLong(v);
        } catch (NumberFormatException e) {
            e.printStackTrace();
        }
        getInstance(context).mPreferences.edit().putLong(key, l).commit();
    }


    /**
     * 取Long
     *
     * @param mPreferences
     * @param key
     * @return
     */
    public static long getLong(SPUtils mPreferences, String key) {
        return mPreferences.mPreferences.getLong(key, -1);
    }


    /**
     * 存储String
     *
     * @param mPreferences
     * @param key
     * @param v
     */
    public static void putLong(SPUtils mPreferences, String key,
                               long v) {
        mPreferences.mPreferences.edit().putLong(key, v).commit();
    }


    public static Long Double2Long(Double d) {
        if (d != null) {
            return Math.round(d);
        }
        return null;
    }


}
